<?php

namespace App\Api\Logic;

use App\Api\Logic\Validate\OrderValidate;
use App\Exceptions\ValidationException;
use App\Helpers\ResponseEnum;
use App\Models\TongGood;
use App\Models\TongOrder;
use App\Models\TongUser;
use App\Models\TongUserAddress;

class OrderLogic extends BaseLogic
{
    /**
     * @return OrderValidate
     */
    private function getValidate():OrderValidate{
        return  new OrderValidate();
    }

    /**
     * @param $params
     * @return void
     * @throws ValidationException
     */
    public function addOrder($params){
        $validate = $this->getValidate();
        if(!$validate->scene('addOrder')->check($params)){
            $this->error($validate->getError());
        }
//        if($params['u_id']!=5067){
//            throw new ValidationException(ResponseEnum::HTTP_ERROR,'系统升级，请稍后再来下单');
//        }

        $result = TongGood::query()->where('id',$params['goods_id'])->exists();
        if(!$result){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'商品不存在');
        }

        $result = TongUserAddress::query()->where('id',$params['address_id'])
            ->where('u_id',$params['u_id'])->exists();
        if(!$result){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'地址错误不存在');
        }
        $goods_info = TongGood::query()->where('id',$params['goods_id'])->first();
        $user_info = TongUser::query()->where('id',$params['u_id'])->first();
        if($goods_info['is_shop_goods']==0 || $goods_info['is_shop_goods']==2){
            //会员商品判断用户身份123
            $buy_order_info_arr = explode(',',$goods_info['buy_order_info']);
            if(!in_array($user_info['identity_id'],$buy_order_info_arr)){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'暂无购买权限，请先升级身份');
            }
        }

    }

    /**
     * @param array $array_merge
     * @return void
     */
    public function pagNotice(array $array_merge)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('payNotice')->check($array_merge)){
            $this->error($validate->getError());
        }
        //查询订单是否存在
        $result = TongOrder::query()->where('id',$array_merge['id'])
            ->where('status',0)
            ->where('u_id',$array_merge['u_id'])->exists();
        if(!$result){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'订单错误');
        }
        //获取用户信息
        $user_info = TongUser::query()->where('id',$array_merge['u_id'])->first();
        if($user_info['pay_password']!=$array_merge['pay_password']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'支付密码错误');
        }
    }

    /**
     * 获取订单详情
     * @param array $params
     * @return void
     */
    public function getOrderInfo(array $params)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getOrderInfo')->check($params)){
            $this->error($validate->getError());
        }
    }

    /**
     * 获取订单列表
     * @param array $params
     * @return void
     */
    public function getOrderList(array $params)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getOrderList')->check($params)){
            $this->error($validate->getError());
        }
    }

    public function wechatPay(array $array_merge)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('wechatPay')->check($array_merge)){
            $this->error($validate->getError());
        }
        //查询订单是否存在
        $result = TongOrder::query()->where('id',$array_merge['id'])
            ->where('status',0)
            ->where('u_id',$array_merge['u_id'])->exists();
        if(!$result){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'订单错误');
        }
    }

    /**
     * 是否允许用户购买多单
     * @param array $params
     * @return void
     */
    public function moreGoods(array $array_merge)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('moreGoods')->check($array_merge)){
            $this->error($validate->getError());
        }
    }

    /**
     * @param array $params
     * @return void
     */
    public function updateOrderAddress(array $params)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('updateOrderAddress')->check($params)){
            $this->error($validate->getError());
        }
        throw new ValidationException(ResponseEnum::HTTP_ERROR,'待开放');
        //获取订单的详情
        $order_query = TongOrder::query()->where('u_id',$params['u_id'])->where('id',$params['id']);
        if(!$order_query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'订单信息有误');
        }
        $order_info = $order_query->first();
        if($order_info['status']!=1){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'此订单无法修改收货地址');
        }
        $user_address_query = TongUserAddress::query()->where('u_id',$params['u_id'])->where('id',$params['address_id']);
        if(!$user_address_query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'地址信息有误');
        }
        if($order_info['order_address_info']['id']==$params['address_id']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'修改地址不能与原地址相同');
        }

    }


}
